import tkinter.ttk as ttk
import gettext
import threading

from unicodedata import numeric

import common.common as common

_ = gettext.gettext


class PageSettingsInput(ttk.Frame):
    def __init__(self, parent):
        super().__init__(parent, width=720, height=480)
        self.page = None
        self.timed_timer = None
        self.timed_interval_cb = None
        self.text_format_cb = None
        self.create_widgets()

    def create_widgets(self):
        row = 0
        title_font = ("TkDefaultFont", 10, "bold")
        title_label = ttk.Label(self, text="Input Settings", font=title_font)
        title_label.grid(row=row, column=0, columnspan=2, sticky="nsew", padx=2, pady=2)
        row += 1

        separator = ttk.Separator(self, orient="horizontal")
        separator.grid(row=row, column=0, columnspan=2, sticky="nsew", padx=2, pady=2)
        row += 1

        timed_interval_label = ttk.Label(self, text="Timing(ms)")
        timed_interval_label.grid(row=row, column=0, sticky="nsew", padx=2, pady=2)
        timed_interval_cb = ttk.Combobox(self)
        timed_interval_cb.state(["readonly"])
        interval_values = [i for i in range(100, 1100, 100)]
        interval_values += [i for i in range(2000, 11000, 1000)]
        interval_values = [_("Disabled"), 50, 70] + interval_values
        timed_interval_cb["values"] = interval_values
        timed_interval_cb.current(0)
        timed_interval_cb.grid(row=row, column=1, sticky="nsew", padx=2, pady=2)
        timed_interval_cb.bind("<<ComboboxSelected>>", self.on_timed_combo_box_selected)
        self.timed_interval_cb = timed_interval_cb
        row += 1

        txt_format_label = ttk.Label(self, text="Format")
        txt_format_label.grid(row=row, column=0, sticky="nsew")
        txt_fmt_cb = ttk.Combobox(self)
        txt_fmt_cb.grid(row=row, column=1, sticky="nsew", padx=2, pady=2)
        txt_fmt_cb.bind("<<ComboboxSelected>>", self.on_txt_fmt_cb_selected)
        common.setup_text_format_for_combobox(txt_fmt_cb)
        self.text_format_cb = txt_fmt_cb
        row += 1

        row_layer = ttk.Frame(self)
        row_layer.grid(row=row, column=0, columnspan=2, sticky="nsew", padx=2, pady=2)
        settings_button = ttk.Button(row_layer, text="Settings")
        settings_button.grid(row=0, column=0, sticky="nsew", padx=2, pady=2)
        send_button = ttk.Button(row_layer, text="Send")
        send_button.config(command=self.input_data)
        send_button.grid(row=0, column=1, sticky="nsew", padx=2, pady=2)
        row_layer.grid_columnconfigure(0, weight=1)
        row_layer.grid_columnconfigure(1, weight=1)
        row += 1

        self.grid_columnconfigure(1, weight=1)

    def on_timed_combo_box_selected(self, event):
        if self.timed_interval_cb is None:
            raise ValueError("No timed interval combo box provided")

        interval = self.timed_interval_cb.get()
        if interval == _("Disabled"):
            self.stop_timer()
            return

        self.restart_timer()

    def on_txt_fmt_cb_selected(self, event):
        if self.page is None:
            raise ValueError("No page provided")

        self.page.setup_text_format_for_entry()

    def restart_timer(self):
        if self.timed_timer and self.timed_timer.is_alive():
            self.timed_timer.cancel()

        interval = float(self.timed_interval_cb.get()) / 1000.0
        self.timed_timer = threading.Timer(interval, self.input_data_then_restart_timer)
        self.timed_timer.daemon = True
        self.timed_timer.start()

    def stop_timer(self):
        if self.timed_timer and self.timed_timer.is_alive():
            self.timed_timer.cancel()

    def input_data_then_restart_timer(self):
        self.input_data()
        self.restart_timer()

    def input_data(self):
        if self.page is None:
            raise ValueError("No page provided")
        self.page.input_data()

    def text_format(self):
        if self.text_format_cb is None:
            raise ValueError("No text format combo box provided")
        return self.text_format_cb.get()
